package com.szholly.pro.watergas_server.ps.android.controller;

import java.util.List;

import javax.servlet.ServletContext;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ServletContextAware;

import com.szholly.pro.watergas_server.ps.android.bean.QcTaskTypeRow;
import com.szholly.pro.watergas_server.ps.android.common.ResultUtils;
import com.szholly.pro.watergas_server.ps.android.common.ValidUtils;
import com.szholly.data.general.RowBase;

/**
 * 质控控制器
 *
 *
 * 2017/3/30 <br/>
 *
 * @author Joey Huang QQ:273179370）
 */
@Controller
@RequestMapping("/android/qc/*")
public class QcControl implements ServletContextAware {
	public static final QcTaskTypeRow SAMPLE = new QcTaskTypeRow("001", "采样", "3a8986d1-2a8d-482c-b09e-fa7db8dqc001");
	public static final QcTaskTypeRow LAB = new QcTaskTypeRow("002", "实验", "3a8986d1-2a8d-482c-b09e-fa7db8dqc002");
	public static final QcTaskTypeRow DATA_CONTRAST = new QcTaskTypeRow("003", "在线数据比对", "3a8986d1-2a8d-482c-b09e-fa7db8dqc003");
	public static final QcTaskTypeRow LOCACLE_CHECK = new QcTaskTypeRow("004", "现场考核", "3a8986d1-2a8d-482c-b09e-fa7db8dqc004");
	public static final QcTaskTypeRow BLIND = new QcTaskTypeRow("005", "盲样考核", "3a8986d1-2a8d-482c-b09e-fa7db8dqc005");
	public static final QcTaskTypeRow SPOT = new QcTaskTypeRow("006", "抽测比对", "3a8986d1-2a8d-482c-b09e-fa7db8dqc006");
	public static final QcTaskTypeRow BUILD_BLIND = new QcTaskTypeRow("007", "发布盲样考核", "3a8986d1-2a8d-482c-b09e-fa7db8dqc005");
	public static final QcTaskTypeRow BUILD_SPOT = new QcTaskTypeRow("008", "发布抽测比对", "3a8986d1-2a8d-482c-b09e-fa7db8dqc006");

	public static final QcTaskTypeRow LOCACLE_CHECK_LAB = new QcTaskTypeRow("004-001", "实验室现场检查","3a8986d1-2a8d-482c-b09e-fa7db8dqc007");
	public static final QcTaskTypeRow LOCACLE_CHECK_SAMPLE = new QcTaskTypeRow( "004-002","采样现场检查", "3a8986d1-2a8d-482c-b09e-fa7db8dqc008");
	public static final QcTaskTypeRow LOCACLE_CHECK_OPERATION = new QcTaskTypeRow( "004-004", "运营维护检查","3a8986d1-2a8d-482c-b09e-fa7db8dqc010");

	//批次
	public static final QcTaskTypeRow BATCH = new QcTaskTypeRow( "虚拟001", "批次","00000000-0000-0000-0000-000000000001");


	public static final QcTaskTypeRow[] TASK_ARRAY = new QcTaskTypeRow[] { SAMPLE, LAB, DATA_CONTRAST, LOCACLE_CHECK, BLIND, SPOT, BUILD_BLIND,
			BUILD_SPOT, LOCACLE_CHECK_LAB, LOCACLE_CHECK_SAMPLE, LOCACLE_CHECK_OPERATION };

	private static final String TASK_LOCACLE_CHECK_LAB_TABLE_1 = "10101";
	private static final String TASK_LOCACLE_CHECK_SAMPLE_TABLE_1 = "10201";
	private static final String TASK_LOCACLE_CHECK_SAMPLE_TABLE_2 = "10202";
	private static final String TASK_LOCACLE_CHECK_SAMPLE_TABLE_3 = "10203";
	private static final String TASK_LOCACLE_CHECK_OPERATION_TABLE_1 = "10301";

	// 特殊公司
	public static final String COMPANY_MONITOR_STATION_NAME = "深圳市环境监测中心站";
	public static final String COMPANY_MANAGER_TYPE = "qc_manager_company";
	public static final String COMPANY_MONITOR_STATION_CODE = "SZHJJCZXZ";

	// 字典项--监测类型
	public static final String DOMAIN_MONITOR_TYPE = "质控监测类型";

	@SuppressWarnings("unused")
	private ServletContext servletContext;

	private QcService mService = QcService.get();

	@Override
	public void setServletContext(ServletContext arg0) {
		this.servletContext = arg0;
	}

	/**
	 * 获取任务界面列表
	 */
	@RequestMapping("/getTaskTypeData")
	@ResponseBody
	public String getTaskTypeData(String token, String userCode) {
		try {
			ValidUtils.validateToken(token);
			ValidUtils.hasText(userCode, "用户ID不能为空");
			RowBase taskTypeData = mService.getTaskTypeData(userCode);
			return ResultUtils.getToastResult(taskTypeData);
		} catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}

	/**
	 * 获取任务界面列表
	 */
	@RequestMapping("/getCompanyData")
	@ResponseBody
	public String getCompanyData(String token, String userCode, String taskTypeCode) {
		try {
			ValidUtils.validateToken(token);
			ValidUtils.hasText(userCode, "用户ID不能为空");
			List<RowBase> list = mService.getCompanyData(userCode, taskTypeCode);
			return ResultUtils.getToastResult(list);
		} catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}

	/**
	 * 获取任务界面列表
	 */
	@RequestMapping("/getPointData")
	@ResponseBody
	public String getPointData(String token, String userCode, String taskTypeCode, String companyCode) {
		try {
			ValidUtils.validateToken(token);
			ValidUtils.hasText(userCode, "用户ID不能为空");
			List<RowBase> list = mService.getPointData(taskTypeCode, companyCode);
			return ResultUtils.getToastResult(list);
		} catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}

	/**
	 * 获取任务界面列表
	 */
	@RequestMapping("/getLocaleCheckData")
	@ResponseBody
	public String getLocaleCheckData(String token) {
		try {
			ValidUtils.validateToken(token);
			List<RowBase> list = mService.getLocacleCheckTaskTypeData();
			return ResultUtils.getToastResult(list);
		} catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}

	/**
	 * 获取任务列表
	 */
	@RequestMapping("/getOperationTaskData")
	@ResponseBody
	public String getOperationTaskData(String token, String userId, String taskTypeCode, String companyCode, String pointCode) {
		try {
			ValidUtils.validateToken(token);
			ValidUtils.hasText(userId, "用户ID不能为空");
			ValidUtils.hasText(taskTypeCode, "任务类型编号为空");
			return null;
		} catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}

	/**
	 * 匹配样品编号是否可用，用于实验界面
	 */
	@RequestMapping("/testSampleIdUsable")
	@ResponseBody
	public String testSampleIdUsable(String token, String sampleId, String qcTypeId,String companyCode) {
		try {
			ValidUtils.validateToken(token);
			ValidUtils.hasText(sampleId, "样品编号不能为空");
			ValidUtils.hasText(qcTypeId, "质控类型不能为空");
			ValidUtils.hasText(companyCode, "公司编号不能为空");
			if ("1".equals(qcTypeId)) {// 现场平行样
				return mService.testPXYSampleId(sampleId,companyCode);
			} else {
				return mService.testNormalId(sampleId,companyCode);
			}
		} catch (Exception e) {
			return ResultUtils.getToastResult(e);
		}
	}

	public static String getTaskGuid(String taskTypeCode) throws Exception {
		for (QcTaskTypeRow taskTypeRow : TASK_ARRAY) {
			if (taskTypeRow.equals(taskTypeCode)) {
				return taskTypeRow.getJobId();
			}
		}
		throw new Exception("任务类型编号有误");
	}

	@Deprecated
	public static String getLocaleCheckTableName(String taskTypeCode) throws Exception {
		throw new Exception("无法获取现场考核子表表名");
	}

	public static String getTaskTypeCode(String jobId) throws Exception {
		for (QcTaskTypeRow taskTypeRow : TASK_ARRAY) {
			if (taskTypeRow.getJobId().equals(jobId)) {
				return taskTypeRow.getId();
			}
		}
		throw new Exception("无法获取现场考核子表表名");
	}

	public static boolean isLocacleChildCheck(String taskTypeCode) {
		if (LOCACLE_CHECK_LAB.equals(taskTypeCode)) {
			return true;
		} else if (LOCACLE_CHECK_OPERATION.equals(taskTypeCode)) {
			return true;
		} else if (LOCACLE_CHECK_SAMPLE.equals(taskTypeCode)) {
			return true;
		} else {
			return false;
		}
	}

	public static String getDomainTableCode(String taskTypeCode, String companyCode) throws Exception {
		if (LOCACLE_CHECK_LAB.equals(taskTypeCode)) {
			return TASK_LOCACLE_CHECK_LAB_TABLE_1;
		} else if (LOCACLE_CHECK_OPERATION.equals(taskTypeCode)) {
			return TASK_LOCACLE_CHECK_OPERATION_TABLE_1;
		} else if (LOCACLE_CHECK_SAMPLE.equals(taskTypeCode)) {
			if ("001".equals(companyCode) || "002".equals(companyCode) || "003".equals(companyCode) || "004".equals(companyCode)) {
				return TASK_LOCACLE_CHECK_SAMPLE_TABLE_1;
			} else if ("005".equals(companyCode) || "006".equals(companyCode)) {
				return TASK_LOCACLE_CHECK_SAMPLE_TABLE_2;
			} else if ("007".equals(companyCode)) {
				return TASK_LOCACLE_CHECK_SAMPLE_TABLE_3;
			} else {
				throw new Exception("公司编号有误");
			}
		} else {
			throw new Exception("任务类型编号有误");
		}
	}


	public static QcTaskTypeRow getTaskTypeRow(String taskTypeCode) throws Exception {
		if (LAB.getId().equals(taskTypeCode)) {
			return LAB;
		}else if (SAMPLE.getId().equals(taskTypeCode)) {
			return SAMPLE;
		}else if (DATA_CONTRAST.getId().equals(taskTypeCode)) {
			return DATA_CONTRAST;
		}else if (LOCACLE_CHECK.getId().equals(taskTypeCode)) {
			return LOCACLE_CHECK;
		}else if (LOCACLE_CHECK_LAB.getId().equals(taskTypeCode)) {
			return LOCACLE_CHECK_LAB;
		}else if (LOCACLE_CHECK_OPERATION.getId().equals(taskTypeCode)) {
			return LOCACLE_CHECK_OPERATION;
		}else if (LOCACLE_CHECK_SAMPLE.getId().equals(taskTypeCode)) {
			return LOCACLE_CHECK_SAMPLE;
		}else if (BUILD_BLIND.getId().equals(taskTypeCode)) {
			return BUILD_BLIND;
		}else if (BUILD_SPOT.getId().equals(taskTypeCode)) {
			return BUILD_SPOT;
		}else if (BLIND.getId().equals(taskTypeCode)) {
			return BLIND;
		}else if (SPOT.getId().equals(taskTypeCode)) {
			return SPOT;
		}
		throw new Exception("任务类型编号有误");
	}
}
